package com.zhang.teachingcommunity_spring.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.teachingcommunity_spring.mapper.CourseMapper;
import com.zhang.teachingcommunity_spring.mapper.entity.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseService {
    @Autowired
    private CourseMapper courseMapper;


    public  List<Course> searchCourse(String courseName) {
        QueryWrapper<Course> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.like("coursename", courseName);
        List<Course> courses = courseMapper.selectList(QueryWrapper);
        return courses;
    }
    public IPage<Course> getCourseList(Course course, Page Ipage,Integer sort) {
        QueryWrapper<Course> userQueryWrapper = new QueryWrapper<>();

        if(course.getSchoolId() != null && !(course.getSchoolId().equals(""))) {
            userQueryWrapper.eq("school_id", course.getSchoolId());
        }
        if(course.getSubjectId() != null && !(course.getSubjectId().equals(""))) {
            userQueryWrapper.eq("subject_id", course.getSubjectId());
        }
        if(course.getCoursename() != null && !(course.getCoursename().equals(""))) {
            userQueryWrapper.eq("coursename", course.getCoursename());
        }
        if(sort!=null && sort.equals("") && sort==2) {
            userQueryWrapper.orderByDesc("create_time");
        }
        userQueryWrapper.isNotNull("course_img");
        IPage<Course> page = new Page<>(Ipage.getCurrent(),Ipage.getSize());
        IPage<Course> userIPage = courseMapper.selectPage(page, userQueryWrapper);
        return userIPage;
    }

}
